/*******************************************************************************
 * 主要存放header.jsp中使用的js
 * 
 * @site http://www.dianziq.com/
 * @author Administrator
 *******************************************************************************/

$(function(){
	initSearchBox();
	bodyscroll();
	searchBtn();
	//给文本框输入换行时自动变高
	$("textarea[data-name='autoheight']").bind("keyup",function(){
		var $obj = $(this);
		var content = $obj.val();
		Wenda.textareaAutoHeight(content,$obj);
	});
	Wenda.imgSizeChange();//图片缩放
	//让IE支持placeholder属性
	try{
		$('input[placeholder]').placeholder();
	}catch(e){}
	initPlaceholder();
});

/**
 * 初始化所有input框，点击input框，将placeholder去掉，不使用火狐的placeholder
 */
function initPlaceholder(){
	$('input[placeholder]').each(function(){
		$(this).attr("placeholder1",$(this).attr("placeholder"));
	});
	$('input[placeholder]').click(function(){
		$(this).attr("placeholder","");
	});
	$('input[placeholder]').bind("focusout",function(){
		var $this = $(this);
		if($this.val()==""){
			$(this).attr("placeholder",$(this).attr("placeholder1"));
		}
	});
}


/**
 * 点击搜索按钮
 */
function searchBtn(){
	$("#search-btn").bind("click",function(){
		//对特殊字符进行编码处理后传递。
		var _key = encodeURIComponent($("#zdq-search").val());
		var url = Constans.BASE_PATH + "search/searchindex.action?q=" + _key + "&searchparam=question";
		$("#enter_Form").attr("method","post");
		$("#enter_Form").attr("action",url);
		$("#enter_Form").submit();
	});
}

/**
 * Body的滚动条事件
 */
function bodyscroll(){
	$(window).bind("scroll",function(){
		if(scrollEvent != "") {
			try{
				eval('(' + scrollEvent + ')');
			}catch(e){}
		}
	});
}

// ------------- GLOBAL VAR --------------

var searchBoxFlag = false;
var downlistObj = null;
var scrollEvent = "";
var $dzqobjtip = {};

/**
 * 初始化搜索框
 */
function initSearchBox(){
	
	var $sbox = $("#zdq-search").parent();
	$dzqobjtip = $sbox;
	try{
		var width = $sbox.width() * 2;
		downlistObj = $("#zdq-search").wendaDropDrownList({
			"width" : width,
			"top" : $sbox.offset().top + $sbox.height(),
			"left" : $sbox.offset().left,
			"searchFun" : "sendSearchData"
		});
	}catch(e){}
	
	scrollEvent = "downlistObj.setConfig({\"top\":$dzqobjtip.offset().top+$dzqobjtip.height(),\"left\":$dzqobjtip.offset().left})";
	
	//对搜索框中的默认提示操作
	$("#zdq-search").bind("click",function(){
		if(Constans.SEARCHBOX_DEF_VALUE == this.value){
			this.value = "";
		}
		scrollEvent = "downlistObj.setConfig({\"top\":$dzqobjtip.offset().top+$dzqobjtip.height(),\"left\":$dzqobjtip.offset().left})";
	});
	
	$( "#zdq-search" ).bind("focusout",function(){
		var val = this.value;
		if($.trim(val) == "" && $.trim(val).length == 0){
			this.value = Constans.SEARCHBOX_DEF_VALUE;
		}
	});
}

/**
 * 发送搜索请求
 * 
 * @param keyword
 */
var _searchTimeout;
function sendSearchData(keyword){
	if(_searchTimeout){
		clearTimeout(_searchTimeout);
	}
	_searchTimeout = setTimeout(function(){
		Wenda.post(Constans.BASE_PATH+"json/searchJsonAction!searchBox.action", {
			"keyword":keyword
		},
		function(datas){
			searchListCallBack(datas,keyword,"all");
		}, 'json');
	},500);
}

/**
 * 搜索回调
 * 
 * @param datas
 * @param keyword
 * @param type
 */
function searchListCallBack(datas,keyword,type){
	var html = "";
	if(type == "all"){
		if(datas.suggestionItems != null){
			for(var i=0;i<datas.suggestionItems.length;i++){
				html += parseSuggestData(datas.suggestionItems[i]);
			}
		}
	}else if(type == "question"){
		if(datas.questions != null && datas.questions.length > 0){
			html += '<div class="xip" style="-moz-user-select: none;background: none repeat scroll 0 0 #D2E1E8 !important;color: ;">您想问的是不是下面的问题？'
				+'<a href="javascript:;" title="关闭" name="close" id="closeSearchList" style="-moz-user-select: none;">'
				+'不是&nbsp;&nbsp;<img src="../img/close.gif"/>&nbsp;</a></div>';
			for(var i=0;i<datas.questions.length;i++){
				html += parseQuestionsData(datas.questions[i]);
			}
			searchQTitleObj.setHtml({"html":html,"keyword":keyword});
		}
	}else if(type == "tag" || type == "editTag"){
		var fun = type == "tag" ? 'addTagsHtml':'editTagsHtml';
		if(datas.tagSuggestionItems != null){
			for(var i=0;i<datas.tagSuggestionItems.length;i++){
				html += parseTagsData(datas.tagSuggestionItems[i],fun);
			}
		}
		addQTagsListObj.setHtml({"html":html,"keyword":keyword});
	}
	
	if(type == "all"){
	//对特殊字符进行编码处理后传递。
	var _key = encodeURIComponent($("#zdq-search").val());
	html += '<div class="ac-row" style="-moz-user-select: none;">查看搜索结果：'
		+'<a href="'+Constans.BASE_PATH+'search/searchindex.action?q='+_key+'&searchparam=question" style="-moz-user-select: none;">'
		+'<b class="ac-highlighted" style="-moz-user-select: none;">'+keyword+'</b></a>'
		+'</div>';
		if(downlistObj)downlistObj.setHtml({"html":html,"keyword":keyword});
	}	
}

/**
 * 拼装html
 * 
 * @param tag
 * @returns {String}
 */
function parseTagsData(tag,fun){
	if(tag == null)return "";
	var html = "";
	html += '<div class="ac-row" role="option" style="-moz-user-select: none;">'
		+'<img src="'+Constans.IMG_BASE_URL+"m_"+tag.img+'" class="xun xjw" style="-moz-user-select: none;width:25px;height:25px" onerror="this.src=\''+Constans.IMG_BASE_URL+'m_tag_default_img.jpg\'">'
		 +'<a href="javascript:'+fun+'(\''+(tag.name||"")+'\');" class="xyl" style="-moz-user-select: none;">'+(tag.name||"")+'</a>'
		 +'<span class="xfr" style="-moz-user-select: none;"><!--又称'
		 +'<b class="ac-highlighted" style="-moz-user-select: none;">g</b>ay，-->'+tag.fc+'个热门问答 '
		 +'</span>'
		 +'</div>';
	return html;
}

/**
 * 拼装html
 * 
 * @param data
 * @returns {String}
 */
function parseSuggestData(data){
	var html = "";
	if(data.type == 1){
	html += '<div class="ac-row" style="-moz-user-select: none;">'
		 +'<a href="'+Constans.BASE_PATH+'tag/tagAction!tagindex.action?tagName='+data.id+'" class="xyl" style="-moz-user-select: none;">'+(data.caption==null?"":data.caption)+'</a>'
		 +'<span class="xfr" style="-moz-user-select: none;"><!--又称'
		 +'<b class="ac-highlighted" style="-moz-user-select: none;">g</b>ay，-->'+data.count+' 个热门问答'
		 +'</span>'
		 +'</div>';
	}else if(data.type == 2){
		html += '<div class="ac-row" style="-moz-user-select: none;">'
			 +'<a class="" title="'+ (data.caption==null?"":data.caption) +'" href="'+Constans.BASE_PATH+'user/userAction!getUser.action?currentId='+data.id+'" style="-moz-user-select: none;">'
			 +'<img src="'+Constans.IMG_BASE_URL+"s_"+data.img+'" class="xun" style="-moz-user-select: none;width:25px;height:25px" onerror="this.src=\''+Constans.IMG_BASE_URL+'s_user_default_img.jpg\'">'
			 +'<span title="'+ (data.caption==null?"":data.caption) +'" class="" style="-moz-user-select: none;">'
			 +(data.caption==null?"":data.caption)+'&nbsp;&nbsp;</span>'
			 +'<span class="xaw xps" style="-moz-user-select: none;">'+(data.detail==null?"":data.detail)+'</span></a></div>';
	}else if(data.type == 3){
		var method = "fetchQuestion";
		var v="答案";
		if(data.ctype > 0){
			method = "fetchShareInfo";
			v="评论";
		}
		html += '<div class="ac-row" style="-moz-user-select: none;">'
			 +'<a href="'+Constans.BASE_PATH+'question/question!'+method+'.action?qid='+data.id ;
		if (data.ctype > 0) {
			html += '&ctype=' + data.ctype ;
		}
		html += '" style="padding: 0 10px;" style="-moz-user-select: none;">'
			 +(data.ctype == 1?"分享：":"")+data.caption+'</a>'
			 +'<span class="" style="-moz-user-select: none;">'+data.count+' 个'+v+'</span></div>';
	}
	else if(data.type == 4){
	 
		html += '<div class="ac-row" style="-moz-user-select: none;">'
			 +'<a href="'+Constans.BASE_PATH+'party/detail.action?pid='+data.id+'" style="padding: 0 10px;" style="-moz-user-select: none;">'
			 +"活动："+data.caption+'</a>'
			 +'<span class="" style="-moz-user-select: none;">'+data.count+' 个报名</span></div>';
	}
	return html;
}
/**
 * 站内邀请，搜索用户
 * 
 * @param userName
 */
function searchSiteInnerUsers(userName){
		if(userName!=null||userName.trim()!=''){
			var userName={'sreachUserName':userName};
			jQuery.ajax({
				type: 'post',
				url: basePath+'notify/msg!searchUserTosendEmail.action',
				data: userName,
				success:  function(data) {
					builtHtml(data);
				},
				error:function(data){
					builtHtml(data);
				},
				dataType: 'json'
			});
		}
		
		function builtHtml(data){
			var obj=eval('('+data.responseText+')');
			var i=0;
			var html='';
			for(var   n   in   obj.users){
				var index=i++;
				var name=obj.users[index].name;
				var id=obj.users[index].id;
				var detail=obj.users[index].detail;
				var img=obj.users[index].img;
				
				html += '<div class="ac-row" style="-moz-user-select: none;">'
					 +'<a class="" title="'+name+'" onclick="addUsersHtml('+id+',\''+name+'\');" href="javascript:;" style="-moz-user-select: none;">'
					 +'<img src="'+Constans.IMG_BASE_URL+"s_"+img+'" class="xun xjw" style="-moz-user-select: none;width:25px;height:25px" onerror="this.src=\''+Constans.IMG_BASE_URL+'s_user_default_img.jpg\'">'
					 +'<span title="'+name+'" class="" style="-moz-user-select: none;">'
					 +(name==null?"":name)+'</span>'
					 +' <span class="xaw xps" style="-moz-user-select: none;">'+(detail==null?"":detail)+'</span></a></div>';
			}
			sendInnerUserLisObj.setHtml({"html":html});
		}
}

/**
 * 添加用户html
 */
function addUsersHtml(id,name){
	if(id != User.id && id > 0 && $("#addQ_tagList").html().indexOf("rmid-"+id) < 0){
		var tagHtml = '<div class="xrk"><span>'+name+'</span>'
					+'	<a name="remove" class="xd xsl" href="javascript:;" id="rmid-'+id+'"></a>'
					+'</div>';
		var $tagList = $("#addQ_tagList");
		$tagList.append(tagHtml);
		$("#addQ_tags").val("");
		$tagList.find("a").unbind("click").bind("click",function(){
			$(this).parent().remove();
		});
	}
}

/**
 * 拼装html
 * 
 * @param question
 * @returns {String}
 */
function parseQuestionsData(question){
	var html = "";
	html = '<div class="ac-row" id=":8" role="option" style="-moz-user-select: none;">'
		+'<a href="'+basePath+'question/question!fetchQuestion.action?qid='+question.qid+'" id="rlq-'+question.qid+'" name="rlq" class="xu">'+question.qtitle+'</a>'
		// +'<span class="xer" style="-moz-user-select: none;">问题</span>'
		+'</div>';
	return html;
}